package com.beyond.utils;

import com.beyond.constant.ApplicationConstant;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 工具类
 * 
 *
 */
public class Utils {

	public final static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");

	/**
	 * 计算时间差--精确到秒
	 * 
	 * @param begin 开始时间
	 * @param end 结束时间
	 * @return int 秒数
	 */
	public static Integer difference(Date begin, Date end) {
		long between = (end.getTime() - begin.getTime());
		return (int) Math.ceil(between / 1000);
	}
	
	
	
	/**
	 * 将日期格式化指定的格式字符串
	 * @param date
	 * @return
	 */
	public static String formatDateToString (Date date){
		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String strDate = format.format(date);
		
		return strDate;
		
	}

	/**
	 * 返回时间戳 yyyyMMddHHmmssSSS
	 * 
	 * @param date
	 * @return
	 */
	public static String timeStamp(Date date) {
		return dateFormat.format(date);
	};

	/**
	 * 生成随机字符串
	 * 
	 * @param length
	 * @return
	 */
	public static String getRandomString() {
		// length表示生成字符串的长度
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 20; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * 将当前时间设置成为当前最后时间点
	 * 
	 * @param date
	 * @return date
	 */
	public static Date createCurrentDate(Date date) {
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(date);
		
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		
		//如果时间大于当天17点，则将工单有效时间设置为第二天的23点55分59秒
		if (hour >= ApplicationConstant.VALIDATE_TIME_LIMIT) {
			
			int day = calendar.get(Calendar.DAY_OF_MONTH) +1;
			
			calendar.set(Calendar.DAY_OF_MONTH,day);
		}
		
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		
		calendar.set(Calendar.MINUTE, 55);
		
		calendar.set(Calendar.SECOND, 59);
		
		return calendar.getTime();
	}

	// 通过utf-8编码程序
	public static String encodeString(String context) throws Exception {
		if (context == null || "".equals(context)) {
			return "";
		}
		BASE64Encoder encode = new BASE64Encoder();

		String s = encode.encode(context.getBytes("UTF-8"));
		return s;

	}

	// 解码
	public static String decodeString(String context) throws Exception {
		if (context == null || "".equals(context)) {
			return "";
		}
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] btye = decoder.decodeBuffer(context);
		String decode = new String(btye, "UTF-8");
		return decode;
	}
	
	
	//获取前一天时间
	public static  Date reterverDate(Date date,int status) {
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTime(date);
		
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		
		if (status == 0) {
			
		  day  = day-1;
		  
		}
		
		calendar.set(Calendar.DAY_OF_MONTH,day);
		
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		Date d = calendar.getTime();
	    
		return d;
	}
	
	
	
	public static String md5 (String text) {
		
		return DigestUtils.md5Hex(text).toUpperCase();
	}
	
	
	public static String base64 (String text) {
		byte[] b = text.getBytes();
		Base64 base64 = new Base64();
		b = base64.encode(b);
		String s = new String(b);
		return s;
	}
	
	
	public static String getDateTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(new Date());
	}
	
	
	

}
